const { Controller } = require('egg');

class NoticeController extends Controller {
  async list() {
    const ctx = this.ctx;
    try {
      const value = ctx.request.body;

      const {title, type, content, order, method, pageIndex, pageSize} = await ctx.app.rule.notice.list(value);

      const data = await ctx.service.list(title, type, content, order, method, pageIndex, pageSize);

      ctx.status = 200;
      ctx.sendSuccess({data});
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({msg: error.message});
      return
    }
  }

  async info() {
    const ctx = this.ctx;
    try {

      const value = await ctx.query;
      const { id } = await ctx.app.rule.notice.info(value);

      const data = await ctx.service.notice.info(id);

      ctx.status = 200;
      ctx.sendSuccess({data});
      return;

    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({msg: error.message});
      return;
    }
  }

  async send() {
    const ctx = this.ctx;
    try {
      const value = ctx.request.body;

      const { path, msg } = await ctx.app.rule.notice.insert(value);

      await ctx.service.notice.insert(path, msg);

      ctx.status = 200;
      ctx.sendSuccess();
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({msg: error.message});
      return;
    }
  }
}

module.exports = NoticeController;
